/**
 * 处理联级选择器赋值
 * @param {Object} list
 * @param {Object} id
 */
export function cascaderIdSelector(list,id){
	if(!list||!id) return [];
	var pathList = addPath(list,null);
	if(!pathList) return [];
	var findObj = cascaderFindObject(pathList,id)
	if(findObj!=null){
		return findObj.path;
	}
	return [];
}
//添加路径
function addPath(list,pPath){
	if(!list) return [];
	for (var i = 0; i <list.length; i++) {
		var path  = [];
		if(pPath!=null){
			for (var j = 0; j <pPath.length; j++) {
				path.push(pPath[j])
			}
		}

		path.push(list[i].value);
		list[i].path=path;
		if(list[i].children!=null){
			list[i].children=addPath(list[i].children,list[i].path);
		}
	}
	return list;
}


//获取子级
var cascader_Sel_AllList=[];
export function cascaderGetChildren(list,id){
	cascader_Sel_AllList =[];
	if(!list||!id) return [];
	//查找对象
	var obj = cascaderFindObject(list,id);
	//console.log(obj.label)
	if(obj!=null){
		cascader_Sel_AllList.push(obj);
		cascaderFindAllChildren(obj.children);
	}
	return cascader_Sel_AllList;
}
//递归方法通过ID寻找对象
export function cascaderFindObject(list,id){
	if(!id||!list) return null;
	//只有父级别
	for (var i = 0; i <list.length; i++) {
		if(list[i].value==id){
			return list[i];
		}
	}
	//有子集
	var childrenList = [];
	for (var i = 0; i <list.length; i++) {
		var children =  list[i].children;
		if(children!=null){
			for(var j = 0; j <children.length; j++){
				childrenList.push(children[j])
			}
		}
	}
	return cascaderFindObject(childrenList,id);
}

function cascaderFindAllChildren(children){
	if(children!=null&&children.length>0){
		for (var i = 0; i <children.length; i++) {
			cascaderFindAllChildren(children[i].children);
			cascader_Sel_AllList.push(children[i]);

		}
	}
}




/**
 * @param {Object} selList 被选择的数组
 */
export function getcascaderId(selList){
	if(!selList) return "";
	if(selList.length>0) return selList[selList.length-1];
	return '';
}
/**
 * @param {Object} list
 * @param {Object} id
 */
export function idToValue(list,id){
	if(!list||!id) return null;
	//只有父级
	for (var i = 0; i <list.length; i++) {

		if(list[i].value==id) {
			return list[i].label;
		}
	}
	//有子集
	var childrenList = [];
	for (var i = 0; i <list.length; i++) {
		var children =  list[i].children;
		if(children!=null){
			for(var j = 0; j <children.length; j++){
				childrenList.push(children[j])
			}
		}

	}
	return idToValue(childrenList,id);
}
/**
 * list 转json
 */
export function listToJson(list){
	return JSON.stringify(list)
}
/**
 * idToName
 * @param {Object} list
 * @param {Object} id
 */
export function idToName(list,id){
	if(!list||!id) return null;
			for (var i = 0; i <list.length ; i++) {
				if(list[i].id==id){
					return list[i].name;
				}
			}
}
var getAllId_ids =[];
export function getAllId(list){
	getAllId_ids =[];
	pushId(list);
	return getAllId_ids;
}
function pushId(list){
	for (var i = 0; i <list.length ; i++) {
		getAllId_ids.push(list[i].id);
		if(list[i].children!=null){
			pushId(list[i].children);
		}
	}
}

/**
 * 移除ids最后的逗号
 * @param {Object} ids
 */
export function removeIdsdot(ids){
	return (ids.substring(ids.length - 1) == ',') ? ids.substring(0, ids.length - 1) : ids;
}
//判断非空
export function isNotNull(value){
	return (value!=undefined&&value!=null&&value!=''&&value!=NaN);
}

/**
*map转换为json
*/
export function mapToJson(map){
	return JSON.stringify(strMapToObj(map));
}
export function strMapToObj(strMap){
    let obj= Object.create(null);
    for (let[k,v] of strMap) {
      obj[k] = v;
    }
    return obj;
}
//处理新增的（合同，任务单，添加列表专用）
export function dealNewRecords(list){
	var newRecords = [];
	for (var i = 0; i <list.length ; i++) {
		if(list[i].id==null||list[i].id==''){
			newRecords.push(list[i]);
		}
	}
	return newRecords;
}
//计算列表总金额

//计算总金额
export function conutTotalAmount(list){
	//循环列表
	var tamount = 0.0;
	for (var j = 0; j <list.length ; j++) {
		var amount = list[j].amount;
			if(!amount){
				amount = 0;
			}
		var discount = list[j].discount;
			if(!discount){
				discount = 0;
			}
		tamount+=parseFloat(amount)-parseFloat(discount);
	}
	return tamount;
}

//隐藏手机号
export function shortMobile(list){
	for (var i = 0; i <list.length; i++) {
		var mobile = list[i].mobile;
		if (mobile != "" && mobile.length == 11) {
		    var short = mobile.substring(0, 4) + "***" + mobile.substring(7, 11);
			list[i].shortMobile=short;
		}else{
			list[i].shortMobile=mobile;
		}
	}
	return list;
}

//string转list
export function stringToList(str){
	if(!str) return [];
	str = removeIdsdot(str);
	return str.split(",");
}
//字符串转列表
export function strToList(str,pInt){
	var list = [];
	if(!str) return list;
	var tl = str.split(',');
	for (var i = 0; i <tl.length; i++) {
		if(pInt){
			list.push(parseInt(tl[i]));
		}else{
			list.push(tl[i]);
		}
	}
	return list;
}

//上传列表转String 逗号隔开
export function uploadListToString(uploadList){
	if(!uploadList||uploadList.length==0){
		return "";
	}
	var urls = "";
	for (var i = 0; i <uploadList.length; i++) {
		var url = uploadList[i].response.url;
		urls+=url+",";
	}
	return removeIdsdot(urls)
}
//列表转ID
export function listToIds(list){
	var ids = "";
	for (var i = 0; i <list.length ; i++) {
		ids+=list[i]+",";
	}
	if(ids!=""){
		ids=removeIdsdot(ids);
	}
	return ids;
}


//cjianquan 2019/10/23  el-cascader 获取选中的label
export function getCascaderLabel(ref){
    var separator = ref.separator;
    var checkedValue = ref.checkedValue;
    var checkLen = checkedValue.length;
    if(checkLen<=0){
    	return '';
	}
    var label = ref.inputValue.split(separator)[checkLen-1];
    return label;
}
//cjianquan 2019/10/23 el-cascader 获取选中的value
export function getCascaderValue(ref){
    var checkedValue = ref.checkedValue;
    var checkLen = checkedValue.length;
    if(checkLen<=0){
        return null;
    }
    var value = checkedValue[checkLen-1];
    return value;
}
//两个时间相差几个月
export function  monthDiff(startTime,endTime){
	if(!startTime||!endTime){
		return null;
	}
	var dateStart = new Date(startTime);
	var dateEnd = new Date(endTime);
	dateStart.setTime(new Date(dateStart.getFullYear(), dateStart.getMonth() , 1));
	dateEnd.setTime(new Date(dateEnd.getFullYear(),dateEnd.getMonth() , 1))
	var date2Mon;
	if(dateEnd>=dateStart){
	        date2Mon=0;
	    }else{
	        date2Mon=-1;
	    }
	return (dateEnd.getYear()-dateStart.getYear())*12+dateEnd.getMonth()-dateStart.getMonth()+date2Mon;
}

//cjianquan 2019/11/12
export function getSelectValues(ref,first){
  var selArr = ref.selected;
  var ids = [];
  if(selArr && selArr.length>0){
    for(var i=0;i<selArr.length;i++){
      var sel = selArr[i];
      ids.push(sel.value);
      if(first){
        break;
      }
    }
  }
  return ids.join(",");
}
//cjianquan 2019/11/12
export function getSelectLabels(ref,first){
  var selArr = ref.selected;
  var labels = [];
  if(selArr && selArr.length>0){
    for(var i=0;i<selArr.length;i++){
      var sel = selArr[i];
      labels.push(sel.label);
      if(first){
        break;
      }
    }
  }
  return labels.join(",");
}

